<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/10 0010
 * Time: 上午 9:15
 */

namespace app\admin\controller;


use controller\BasicAdmin;
use service\DataService;
use think\Db;

class ExpertsCate extends BasicAdmin
{
    public $table = 'CategoryModule';
    public function cate(){
        $this->title = '栏目';
        $db = Db::name('CategoryModule')->field('id,name as title,sort,is_deleted,status,create_at')->where(['is_deleted'=>1,'pid'=>16])->order('sort asc,id asc');
        return parent::_list($db,false);
    }

    public function _data_filter(&$data)
    {
        foreach ($data as $k=>$v){
            $experts_cate = Db::name('ExpertsCate')->field('id,desc')->where('pid',$v['id'])->find();
            if ($experts_cate){
                $data[$k]['experts_cate'] = $experts_cate;
            }else{
                $data[$k]['experts_cate']['id'] = '';
                $data[$k]['experts_cate']['desc'] = '';
            }
        }
    }

    public function cate_add(){
        $this->title = '分栏详情';
        return $this->_form('ExpertsCate','cate_add');
    }

    public function _form_filter(&$vo){
        if ($this->request->isPost()){
            if (empty($vo['id']) && empty($vo['pid'])){
                $pid = Db::name('CategoryModule')->insertGetId([
                    'name'=>$vo['title'],
                    'pid'=>16,
                    'create_at'=>date('Y-m-d H:i:s')
                ]);
                $vo['pid'] = $pid;
            }else{
                Db::name($this->table)->where('id',$vo['pid'])->update(['name'=>$vo['title']]);
            }
            if (!empty($vo['footer'])){
                $vo['footer'] = array_values($vo['footer']);
                foreach ($vo['footer'] as $k=>$v){
                    if (isset($v['status']) && $v['status']=='on'){
                        $vo['footer'][$k]['status'] = 1;
                    }else{
                        $vo['footer'][$k]['status'] = 2;
                    }
                }
                $vo['footer'] = json_encode($vo['footer'],320);
            }
        }elseif ($this->request->isGet()){
            $pid = $this->request->get('pid');
            $category = Db::name($this->table)->where('id',$pid)->find();
            $vo['title'] = $category['name'];
            $vo['pid'] = $category['id'];
            empty($vo['footer']) || $vo['footer'] = json_decode($vo['footer'],true);
        }
    }

    /**
     * 分类禁用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        if (DataService::update($this->table)) {
            Db::name('ExpertsCate')->where(['pid'=>explode(',',$this->request->post('id'))])
                ->update([$this->request->post('field')=>$this->request->post('value')]);
            $this->success("分类禁用成功！", '');
        }
        $this->error("分类禁用失败，请稍候再试！");
    }

    /**
     * 分类启用
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        if (DataService::update($this->table)) {
            Db::name('ExpertsCate')->where(['pid'=>explode(',',$this->request->post('id'))])
                ->update([$this->request->post('field')=>$this->request->post('value')]);
            $this->success("分类启用成功！", '');
        }
        $this->error("分类启用失败，请稍候再试！");
    }

    /**
     * 删除分类
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            Db::name('ExpertsCate')->where(['pid'=>explode(',',$this->request->post('id'))])
                ->update([$this->request->post('field')=>$this->request->post('value')]);
            $this->success("分类删除成功！", '');
        }
        $this->error("分类删除失败，请稍候再试！");
    }
}